package com.xy.mainp.home.fragment.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.xy.img.GlideUtil;
import com.xy.mainp.R;
import com.xy.mainp.databinding.ViewAndroidItemBinding;
import com.xy.mainp.model.home.android.HomeResultEntity;
import com.xy.mainp.utils.TimeUtil;
import com.xy.manager.MsgManager;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Created by John on 2016/11/3.
 */

public class ItemHomeView extends FrameLayout implements View.OnClickListener {

    private ViewAndroidItemBinding binding;
    private HomeResultEntity data;

    public ItemHomeView(Context context) {
        this(context, null);
    }

    public ItemHomeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemHomeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setClickable(true);
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_android_item, this, true);
        binding.ivAndroidCollect.setOnClickListener(this);
        binding.ivGifAndroid.setOnClickListener(this);
    }

    public void setData(HomeResultEntity data) {
        this.data = data;
        try {
            binding.setHomeResultEntity(data);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
            long time = dateFormat.parse(data.getPublishedAt()).getTime();
            binding.tvAndroidTime.setText(TimeUtil.friendlyTime(time));
            if (data.getType().equalsIgnoreCase("福利")){
                if (data.getUrl() != null){
                    GlideUtil.loadImage(data.getUrl(),binding.ivGifAndroid);
                    binding.ivGifAndroid.setVisibility(VISIBLE);
                }else {
                    binding.ivGifAndroid.setVisibility(GONE);
                }
            }else {
                if (data.getImages() != null){
                    GlideUtil.loadGifS(data.getImages().get(0),binding.ivGifAndroid);
                    binding.ivGifAndroid.setVisibility(VISIBLE);
                }else {
                    binding.ivGifAndroid.setVisibility(GONE);
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.iv_gif_android){
            if (data.getImages() != null){
                GlideUtil.loadGif(data.getImages().get(0), binding.ivGifAndroid, 0, new RequestListener<String, GifDrawable>() {
                    @Override
                    public boolean onException(Exception e, String model, Target<GifDrawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(GifDrawable resource, String model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                        MsgManager msgManager = new MsgManager(getContext());
                        if (target.getRequest().isRunning()){
                            msgManager.showLoading(true);
                        }else if (target.getRequest().isRunning()){
                            msgManager.showLoading(false);
                        }
                        return false;
                    }
                });
            }
        }else if(view.getId() == R.id.iv_android_collect){

        }
    }
}
